
#################################################################################################

#  Description: Generating Tables and Figures from Study 1 in Fleming, Thomas G. (Forthcoming) 
#               'Partisanship and the Effectiveness of Personal Vote-Seeking', Legislative Studies 
#               Quarterly.
#  Author:      Thomas G. Fleming
#  Date:        18/01/2021
#  Note:        Requires files "study1_randomisation_data.csv" and "study1_analysis_data.csv" 
#               to be saved in same folder

#################################################################################################

rm(list=ls())

library(stargazer)
library(interplot)
library(DescTools)  # for PseudoR2

library(rstudioapi)
current_path <- getActiveDocumentContext()$path
setwd(dirname(current_path))

#################
# Loading data  #
#################

  analysis.data <- read.csv("./study1_analysis_data.csv")
  randomisation.data <- read.csv("./study1_randomisation_data.csv")

#################################################################
# Table 1 - Logit model of probability of supporting incumbent  #
#################################################################

  # creating identical model-specific DVs, for easier formatting of results table

  analysis.data$supported.MP1 <- analysis.data$supported.MP
  analysis.data$supported.MP2 <- analysis.data$supported.MP
  analysis.data$supported.MP3 <- analysis.data$supported.MP
  analysis.data$supported.MP4 <- analysis.data$supported.MP

  # Model 1 (all parties' MPs)
  
  effect.allparties <- glm(supported.MP1 ~
                             drawn +
                             attempts +
                             as.factor(surveyyear),
                           data = analysis.data[(analysis.data$attempts > 0) & # MPs who actually entered the ballot
                                         (analysis.data$standing == "yes") &   # MPs who ran for re-election
                                         (analysis.data$drawn != 2),],         # MPs drawn 0 or 1 times
                           family = "binomial")
  effect.allparties.R2 <- PseudoR2(effect.allparties, which = "McFadden")
  
  # Model 2 (all parties' MPs, including party fixed-effects)
  
  effect.partycontrol <- glm(supported.MP2 ~
                               drawn +
                               attempts +
                               as.factor(MP.party) +
                               as.factor(surveyyear),
                             data = analysis.data[(analysis.data$attempts > 0) &         # MPs who actually entered the ballot
                                                    (analysis.data$standing == "yes") &  # MPs who ran for re-election
                                                    (analysis.data$drawn != 2),],        # MPs drawn 0 or 1 times
                             family = "binomial")
  effect.partycontrol.R2 <- PseudoR2(effect.partycontrol, which = "McFadden")
  
  # Model 3 (only Labour party MPs)
  
  effect.labour <- glm(supported.MP3 ~
                         drawn +
                         attempts +
                         as.factor(surveyyear),
                       data = analysis.data[(analysis.data$attempts > 0) &     # MPs who actually entered the ballot
                                     (analysis.data$standing == "yes") &       # MPs who ran for re-election
                                     (analysis.data$drawn != 2) &              # MPs drawn 0 or 1 times
                                     (analysis.data$MP.party == "labour"),],   # Labour MPs only   
                       family = "binomial")
  effect.labour.R2 <- PseudoR2(effect.labour, which = "McFadden")
  
  # Model 4 (only National party MPs)
  
  effect.national <- glm(supported.MP4 ~
                           drawn +
                           attempts +
                           as.factor(surveyyear),
                         data = analysis.data[(analysis.data$attempts > 0) &     # MPs who actually entered the ballot
                                       (analysis.data$standing == "yes") &       # MPs who ran for re-election
                                       (analysis.data$drawn != 2) &              # MPs drawn 0 or 1 times
                                       (analysis.data$MP.party == "national"),], # National MPs only   
                         family = "binomial")
  effect.national.R2 <- PseudoR2(effect.national, which = "McFadden")
  
  # generating combined regression table
  
  var.order <- c("drawn", "attempts", "Constant")
  
  stargazer(effect.allparties,
            effect.partycontrol,
            effect.labour,
            effect.national,
            type = "html",
            title = "Table 1: Logit model of probability of supporting incumbent",
            out = "./table1.html",
            keep = paste0("^", var.order, "$"),
            order = paste0("^", var.order, "$"),
            covariate.labels = c("Bills drawn",
                                 "Ballot attempts",
                                 "Constant"),
            dep.var.caption = "Voting for incumbent MP",
            dep.var.labels.include = TRUE,
            dep.var.labels = c("All parties", "All parties", "Labour", "National"),
            column.labels = c("(1)", "(2)", "(3)", "(4)"),
            no.space = TRUE,
            model.numbers = FALSE,
            omit.stat = c("n", "ll", "aic"),
            add.lines = list(c("Election FE", "Yes", "Yes", "Yes", "Yes"),
                             c("MP Party FE", "", "Yes", "", ""),
                             c("Observations", nobs(effect.allparties), nobs(effect.partycontrol),
                               nobs(effect.labour), nobs(effect.national)),
                             c("Pseudo R\\textsuperscript{2}",
                               round(effect.allparties.R2, digits = 3),
                               round(effect.partycontrol.R2, digits = 3),
                               round(effect.labour.R2, digits = 3),
                               round(effect.national.R2, digits = 3))))
  
#################################################################
# Table 2 - Logit model of probability of supporting incumbent  #
#################################################################
  
  # Model 5 (adding interaction, but no controls)
  
  interaction.nocontrols <- glm(supported.MP ~
                                  drawn*partisanship +
                                  attempts +
                                  as.factor(surveyyear),
                                data = analysis.data[(analysis.data$attempts > 0) &  # MPs who actually entered the ballot
                                              (analysis.data$standing == "yes") &    # MPs who ran for re-election
                                              (analysis.data$drawn != 2),],          # MPs drawn 0 or 1 times
                                family = "binomial")
  interaction.nocontrols.R2 <- PseudoR2(interaction.nocontrols, which = "McFadden")
  
  # Model 6 (adding interaction and controls)
  
  interaction.controls <- glm(supported.MP ~
                                drawn*partisanship + 
                                attempts +
                                LR.distance +
                                MPs.party.approval +
                                MP.approval +
                                as.factor(surveyyear),
                              data = analysis.data[(analysis.data$attempts > 0) &  # MPs who actually entered the ballot
                                            (analysis.data$standing == "yes") &    # MPs who ran for re-election
                                            (analysis.data$drawn != 2),],          # MPs drawn 0 or 1 times
                              family = "binomial")
  interaction.controls.R2 <- PseudoR2(interaction.controls, which = "McFadden")
  
  # Model 7 (adding interaction, controls, and party fixed effects)
  
  interaction.partyFE <- glm(supported.MP ~
                               drawn*partisanship + 
                               attempts +
                               LR.distance +
                               MPs.party.approval +
                               MP.approval +
                               as.factor(MP.party) +
                               as.factor(surveyyear),
                             data = analysis.data[(analysis.data$attempts > 0) &  # MPs who actually entered the ballot
                                           (analysis.data$standing == "yes") &    # MPs who ran for re-election
                                           (analysis.data$drawn != 2),],          # MPs drawn 0 or 1 times
                             family = "binomial")
  interaction.partyFE.R2 <- PseudoR2(interaction.partyFE, which = "McFadden")
  
  # generating combined regression table
  
  var.order <- c("drawn", "drawn:partisanship", "partisanship", "attempts", 
                 "LR.distance", "MPs.party.approval", "MP.approval", "Constant")
  
  stargazer(interaction.nocontrols,
            interaction.controls,
            interaction.partyFE,
            type = "html",
            title = "Table 2: Logit model of probability of supporting incumbent",
            out = "./table2.html",
            keep = paste0("^", var.order, "$"),
            order = paste0("^", var.order, "$"),
            covariate.labels = c("Bills drawn",
                                 "Bills drawn * Partisanship",
                                 "Partisanship",
                                 "Ballot attempts",
                                 "Left-right distance",
                                 "Party approval",
                                 "MP approval",
                                 "Constant"),
            dep.var.caption = "Voting for incumbent MP",
            dep.var.labels.include = FALSE,
            column.labels = c("(5)", "(6)", "(7)"),
            no.space = TRUE,
            model.numbers = FALSE,
            omit.stat = c("n", "ll", "aic"),
            add.lines = list(c("Election FE", "Yes", "Yes", "Yes"),
                             c("MP Party FE", "", "", "Yes"),
                             c("Observations", nobs(interaction.nocontrols),
                               nobs(interaction.controls), nobs(interaction.partyFE)),
                             c("Pseudo R\\textsuperscript{2}",
                               round(interaction.nocontrols.R2, digits = 3),
                               round(interaction.controls.R2, digits = 3),
                               round(interaction.partyFE.R2, digits = 3))))
  
####################################################################################
# Figure 1 - Marginal effect of bill proposals on probability of incumbent support #
####################################################################################

  interplot(m = interaction.nocontrols, var1 = "drawn", var2 = "partisanship", ci = 0.95)+
    geom_hline(yintercept = 0, linetype = "dashed")+
    theme_classic()+
    labs(x = "\nVoter partisanship", y = "Marginal effect", caption = "")+
    theme(axis.title.x = element_text(face = "bold"),
          axis.title.y = element_text(face = "bold"),
          panel.border = element_rect(colour = "black", fill = NA, size = 0.7),
          panel.background = element_blank(),
          axis.line = element_blank())+
    scale_x_continuous(breaks = c(0,1,2,3), labels = c("None", "Not very strong", "Fairly strong", "Very strong"))
  ggsave("./figure1.jpg", width = 6, height = 4, units = "in")
  
###########################################################
# Table A1 - OLS model of number of bills drawn in ballot #
###########################################################
  
  # Model A1 (government/opposition differences)
  
  randomisation.gov <- lm(drawn ~
                            gov +
                            attempts +
                            as.factor(surveyyear),
                          data = randomisation.data)
  
  # Model A2 (party differences)
  
  randomisation.party <- lm(drawn ~
                              MP.party +
                              attempts +
                              as.factor(surveyyear),
                            data = randomisation.data)
  
  # Model A3 (government/opposition and party differences)
  
  randomisation.both <- lm(drawn ~
                             gov +
                             MP.party +
                             attempts +
                             as.factor(surveyyear),
                           data = randomisation.data)
  
  # generating combined regression table

  var.order <- c("gov", "MP.partyNational", "MP.partyOther", "attempts", "Constant")
  
  stargazer(randomisation.gov,
            randomisation.party,
            randomisation.both,
            type = "html",
            title = "Table A1: OLS model of number of bills drawn in ballot",
            out = "./tableA1.html",
            keep = paste0("^", var.order, "$"),
            order = paste0("^", var.order, "$"),
            covariate.labels = c("Governing party",
                                 "National Party (vs. Labour)",
                                 "Other party (vs. Labour)",
                                 "Ballot attempts",
                                 "Constant"),
            dep.var.caption = "Bills drawn in ballot",
            dep.var.labels.include = FALSE,
            column.labels = c("(A1)", "(A2)", "(A3)"),
            no.space = TRUE,
            model.numbers = FALSE,
            keep.stat = c("n", "rsq"),
            add.lines = list(c("Parliament FE", "Yes", "Yes", "Yes")))
  
#########################################################################################
# Table A2 - Logit model of probability of supporting incumbent (incl. MPs drawn twice) #
#########################################################################################
  
  # Model A4 (adding interaction, but no controls)
  
  interaction.1.twobills <- glm(supported.MP ~
                                  drawn*partisanship +
                                  attempts +
                                  as.factor(surveyyear),
                                data = analysis.data[(analysis.data$attempts > 0) & 
                                              (analysis.data$standing == "yes"),],
                                family = "binomial")
  interaction.1.twobills.R2 <- PseudoR2(interaction.1.twobills, which = "McFadden")
  
  # Model A5 (adding interaction and controls)
  
  interaction.2.twobills <- glm(supported.MP ~
                                  drawn*partisanship +
                                  attempts +
                                  LR.distance +
                                  MPs.party.approval +
                                  MP.approval +
                                  as.factor(surveyyear),
                                data = analysis.data[(analysis.data$attempts > 0) & 
                                              (analysis.data$standing == "yes"),],
                                family = "binomial")
  interaction.2.twobills.R2 <- PseudoR2(interaction.2.twobills, which = "McFadden")
  
  # Model A6 (adding interaction, controls, and party fixed effects)
  
  interaction.3.twobills <- glm(supported.MP ~
                                  drawn*partisanship + 
                                  attempts +
                                  LR.distance +
                                  MPs.party.approval +
                                  MP.approval +
                                  as.factor(MP.party) +
                                  as.factor(surveyyear),
                                data = analysis.data[(analysis.data$attempts > 0) & 
                                              (analysis.data$standing == "yes"),],
                                family = "binomial")
  interaction.3.twobills.R2 <- PseudoR2(interaction.3.twobills, which = "McFadden")
  
  # generating combined regression table
  
  var.order <- c("drawn", "drawn:partisanship", "partisanship", "attempts", 
                 "LR.distance", "MPs.party.approval", "MP.approval", "Constant")
  
  stargazer(interaction.1.twobills,
            interaction.2.twobills,
            interaction.3.twobills,
            type = "html",
            title = "Table A2: Logit model of probability of supporting incumbent (incl. MPs drawn twice)",
            out = "./tableA2.html",
            keep = paste0("^", var.order, "$"),
            order = paste0("^", var.order, "$"),
            covariate.labels = c("Bills drawn",
                                 "Bills drawn * Partisanship",
                                 "Partisanship",
                                 "Ballot attempts",
                                 "Left-right distance",
                                 "Party approval",
                                 "MP approval",
                                 "Constant"),
            dep.var.caption = "Voting for incumbent MP",
            dep.var.labels.include = FALSE,
            column.labels = c("(A4)", "(A5)", "(A6)"),
            no.space = TRUE,
            model.numbers = FALSE,
            omit.stat = c("n", "ll", "aic"),
            add.lines = list(c("Election FE", "Yes", "Yes", "Yes"),
                             c("MP Party FE", "", "", "Yes"),
                             c("Observations", nobs(interaction.1.twobills),
                               nobs(interaction.2.twobills), nobs(interaction.3.twobills)),
                             c("Pseudo R\\textsuperscript{2}",
                               round(interaction.1.twobills.R2, digits = 3),
                               round(interaction.2.twobills.R2, digits = 3),
                               round(interaction.3.twobills.R2, digits = 3))))
  
################################################################################
# Table A3 - Logit model of probability of supporting incumbent (without 2011) #
################################################################################
  
  # Model A7 (adding interaction, but no controls)
  
  interaction.1.without11 <- glm(supported.MP ~
                                   drawn*partisanship +
                                   attempts +
                                   as.factor(surveyyear),
                                 data = analysis.data[(analysis.data$attempts > 0) & 
                                               (analysis.data$standing == "yes") &
                                               (analysis.data$drawn != 2) &
                                               (analysis.data$surveyyear != 2011),],
                                 family = "binomial")
  interaction.1.without11.R2 <- PseudoR2(interaction.1.without11, which = "McFadden")
  
  # Model A8 (adding interaction and controls)
  
  interaction.2.without11 <- glm(supported.MP ~
                                   drawn*partisanship + 
                                   attempts +
                                   LR.distance +
                                   MPs.party.approval +
                                   MP.approval +
                                   as.factor(surveyyear),
                                 data = analysis.data[(analysis.data$attempts > 0) & 
                                               (analysis.data$standing == "yes") &
                                               (analysis.data$drawn != 2) &
                                               (analysis.data$surveyyear != 2011),],
                                 family = "binomial")
  interaction.2.without11.R2 <- PseudoR2(interaction.2.without11, which = "McFadden")
  
  # Model A9 (adding interaction, controls, and party fixed effects)
  
  interaction.3.without11 <- glm(supported.MP ~
                                   drawn*partisanship + 
                                   attempts +
                                   LR.distance +
                                   MPs.party.approval +
                                   MP.approval +
                                   as.factor(MP.party) +
                                   as.factor(surveyyear),
                                 data = analysis.data[(analysis.data$attempts > 0) & 
                                               (analysis.data$standing == "yes") &
                                               (analysis.data$drawn != 2) &
                                               (analysis.data$surveyyear != 2011),],
                                 family = "binomial")
  interaction.3.without11.R2 <- PseudoR2(interaction.3.without11, which = "McFadden")
  
  # generating combined regression table
  
  var.order <- c("drawn", "drawn:partisanship", "partisanship", "attempts", 
                 "LR.distance", "MPs.party.approval", "MP.approval", "Constant")
  
  stargazer(interaction.1.without11,
            interaction.2.without11,
            interaction.3.without11,
            type = "html",
            title = "Table A3: Logit model of probability of supporting incumbent (without 2011)",
            out = "./tableA3.html",
            keep = paste0("^", var.order, "$"),
            order = paste0("^", var.order, "$"),
            covariate.labels = c("Bills drawn",
                                 "Bills drawn * Partisanship",
                                 "Partisanship",
                                 "Ballot attempts",
                                 "Left-right distance",
                                 "Party approval",
                                 "MP approval",
                                 "Constant"),
            dep.var.caption = "Voting for incumbent MP",
            dep.var.labels.include = FALSE,
            column.labels = c("(A7)", "(A8)", "(A9)"),
            no.space = TRUE,
            model.numbers = FALSE,
            omit.stat = c("n", "ll", "aic"),
            add.lines = list(c("Election FE", "Yes", "Yes", "Yes"),
                             c("MP Party FE", "", "", "Yes"),
                             c("Observations", nobs(interaction.1.without11),
                               nobs(interaction.2.without11), nobs(interaction.3.without11)),
                             c("Pseudo R\\textsuperscript{2}",
                               round(interaction.1.without11.R2, digits = 3),
                               round(interaction.2.without11.R2, digits = 3),
                               round(interaction.3.without11.R2, digits = 3))))
  
##########################################################################################
  
  rm(list=ls())
  